<?
require_once $_SERVER['DOCUMENT_ROOT'] . "/kernel/starter.php";
Module::Init('system:structure');
Module::Init('system:filesystem');
BDS::Init();
if(check_action('delete_element', 'post')){
    BDSElements::Delete($_POST['id']);
    Logger::ok('Deleted. Close window');
}

if(check_action('new_element', 'post')){    
    
    unset($_POST['action']);
    $data = $_POST;
    
    $data['date_create'] = $data['date_update'] = date('Y-m-d H:i:s');
    $fields = array();
    $ai = BDSElements::NextId();
    $data['id'] = $ai;
    if(isset($data['fields'])){
        $fields = $data['fields'];
        unset($data['fields']);
    }
    
    if($data['section_id'] == '0'){
        $data['section_id'] = "NULL()";
    }
    if(!$data['code']){
        $data['code'] = 'record_'.$ai;
    }
    if(!$data['title']){
        $data['title'] = $data['code'];
    }
    if($data['type'] == "S"){
        $result = BDSSections::Create($data);
    }else{
        $result = BDSElements::Create($data);
    }
    
    if(!empty($fields)){
        foreach($fields as $field_id => $value){
            $tmp = BDSFields::GetById($field_id);
            $field_type = BDSFields::GetType($tmp['field_type']);
            if($field_type['type'] == 'file'){
                preg_match("#(\.[\w\d]+)$#", $_FILES['fields']['name'][$field_id], $m);                
                $fs_data = array(
                    'value_id' => $field_id,
                    'name' => $_FILES['fields']['name'][$field_id],
                    'type' => $_FILES['fields']['type'][$field_id],
                    'size' => $_FILES['fields']['size'][$field_id],
                    'tmp' => $_FILES['fields']['tmp_name'][$field_id],
                    'path' => "/uploads/site_".$MAIN->site->id."/elements/el_".$data['id']."_".uniqid("file_").$m[1],
                );
                $value = $fs_data['name'];
                Filesystem::UploadFile($fs_data);
            }
            $result = array();
            $result['field_id'] = $field_id;
            $result['value'] = $value;
            $result['parent_id'] = $data['id'];
            $result = BDSFieldValues::Create($result); 
        }
    }/**/
    
    if($result){
        Logger::ok('Ok');
    }else{
        Logger::error('Error');
    }/**/
}
if(check_action('edit_element', 'post')){      
    unset($_POST['action']);
    $data = $_POST;
    
    $data['date_create'] = $data['date_update'] = date('Y-m-d H:i:s');
    $fields = array();
    
    if(isset($data['fields'])){
        $fields = $data['fields'];
        unset($data['fields']);
    }
    $id = $data['id'];
    unset($data['id']);
    $result = BDSRecords::Update($id, $data);
    
    if(!empty($fields)){
        foreach($fields as $field_id => $field){
            $values = BDSFieldValues::GetList(array('field_id' => $field_id, 'parent_id' => $id));
            $tmp = BDSFields::GetById($field_id);
            $field_type = BDSFields::GetType($tmp['field_type']);
            if($field_type['type'] == 'file'){
                preg_match("#(\.[\w\d]+)$#", $_FILES['fields']['name'][$field_id], $m);                
                $fs_data = array(
                    'value_id' => $field_id,
                    'name' => $_FILES['fields']['name'][$field_id],
                    'type' => $_FILES['fields']['type'][$field_id],
                    'size' => $_FILES['fields']['size'][$field_id],
                    'tmp' => $_FILES['fields']['tmp_name'][$field_id],
                    'path' => "/uploads/site_".$MAIN->site->id."/elements/el_".$id."_".uniqid("file_").$m[1],
                );                
                Filesystem::UploadFile($fs_data);
                $field = $fs_data['name'];
            }
            $result = BDSFieldValues::Update($values[0]['id'], array('value' => $field));
        }
    }
    if($result){
        Logger::ok('Ok');
    }else{
        Logger::error('Error');
    }/**/
}
Logger::ShowAll();
?>

<? if (check_action('new_element', 'get') || check_action('new_section', 'get')): ?>
    <form method="post" enctype="multipart/form-data">
        <? set_action('new_element'); ?>
            <?
            $relation = check_action('new_section', 'get') ? "S" : "E";
            $fields = BDSFields::GetList(array('block_id' => $_GET['block_id'], 'relation' => $relation));            
            ?>
        <input type="hidden" name="block_id" value="<?= $_GET['block_id'] ?>">
        <input type="hidden" name="section_id" value="<?= $_GET['section_id'] ?>">
        <input type="hidden" name="type" value="<?=$relation?>">
        <input type="text" name="sort" value="100">
        <input type="text" name="title" value="">
        <input type="text" name="code" value="">
        <? $MAIN->Component("system:formprocessor", false, array(
            'fields' => $fields,
            'parent_id' => $_GET['section_id'],
            'use_form' => false,
            'relation' => $relation,
        ))?>
        <div class="input">
            <input type="submit" value="<?=(check_action('new_element', 'get') ? t('Create new element') : t('Create new section')); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('show_element', 'get') || check_action('show_section', 'get')): ?>
<form method="post" enctype="multipart/form-data">
        <? set_action('edit_element'); ?>
            <?
            $record = BDSRecords::GetByID($_GET['id']);             
            $fields = BDSFields::GetList(array('block_id' => $record['block_id'], 'relation' => $record['type']));
            ?>
        <input type="hidden" name="id" value="<?= $record['id'] ?>">
        <input type="hidden" name="type" value="<?=$record['type']?>">
        <input type="text" name="sort" value="100">
        <input type="text" name="title" value="<?=$record['title'];?>">
        <input type="text" name="code" value="<?=$record['code'];?>">
        <? $MAIN->Component("system:formprocessor", false, array(
            'fields' => $fields,
            'parent_id' => $_GET['id'],
            'use_form' => false,
            'relation' => $record['type'],
        ))?>
        <div class="input">
            <input type="submit" value="<?=($record['type'] == "E" ? t('Edit element') : t('Edit section')); ?>">
        </div>
    </form>
<? endif; ?>

<? if (check_action('delete_element', 'get') && !check_action('delete_element', 'post')): ?>
    <form method="post">
        <? set_action('delete_element'); ?>            
        <input type="hidden" name="id" value="<?= $_GET['id'] ?>">
        <div class="input">
            <input type="submit" value="Delete?">
        </div>
    </form>
<? endif; ?>
